package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.SysMenuDto;
import com.itheima.ydd.mapper.SysMenuMapper;
import com.itheima.ydd.pojo.SysMenu;
import com.itheima.ydd.service.SysMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单管理
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    /**
     * 根据id修改菜单功能
     * @param sysMenu
     * @return
     */
    @Override
    public ResponseEntity<R<String>> updateSysmenu(SysMenu sysMenu) {
        updateById(sysMenu);
        return ResponseEntity.ok(R.success("修改成功!"));
    }

    /**
     * 根据id删除菜单功能
     * @param id
     * @return
     */
    @Override
    public ResponseEntity<R<String>> deleteSysMenu(Integer id) {
        //判断该菜单是否有子菜单,如果有子菜单,不允许删除
        LambdaQueryWrapper<SysMenu> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getPid,id);
        SysMenu one = getOne(wrapper);
        if(!ObjectUtils.isEmpty(one)){
           //不允许删除
            return ResponseEntity.ok(R.err("该菜单下有子菜单,请先删除所有的子菜单!"));
        }
        //允许删除
        removeById(id);
        return ResponseEntity.ok(R.success(null));
    }

    /**
     * 查询全部菜单的方法:三级菜单查询
     *
     * @return
     */
    @Override
    public ResponseEntity<R<List<SysMenuDto>>> findAllData() {
        //先查询出所有一级菜单 pid=0
        LambdaQueryWrapper<SysMenu> wrapperFather=new LambdaQueryWrapper<>();
        wrapperFather.eq(SysMenu::getPid,0);
        List<SysMenu> listOne = list(wrapperFather);
        //将集合转换类型
        List<SysMenuDto> sysMenuDtoList = listOne.stream().map((item) -> {
            SysMenuDto sysMenuDto=new SysMenuDto();
            BeanUtils.copyProperties(item,sysMenuDto);
            return sysMenuDto;
        }).collect(Collectors.toList());
        //根据一级菜单id,查询二级菜单
        for (SysMenuDto sysMenuDto : sysMenuDtoList) {
            Integer id = sysMenuDto.getId();
            LambdaQueryWrapper<SysMenu> wrapperTwo=new LambdaQueryWrapper<>();
            wrapperTwo.eq(SysMenu::getPid,id);
            List<SysMenu> listTwo = list(wrapperTwo);
            //将集合转换类型,并存入sysMenuDtoList集合中对应的元素的集合属性中
            List<SysMenuDto> collect = listTwo.stream().map((item) -> {
                SysMenuDto sysMenuDtoTwo=new SysMenuDto();
                BeanUtils.copyProperties(item,sysMenuDtoTwo);
                //现在根据二级菜单的id,查询三级菜单
                Integer idTwo = item.getId();
                LambdaQueryWrapper<SysMenu> wrapperThree=new LambdaQueryWrapper<>();
                wrapperThree.eq(SysMenu::getPid,idTwo);
                List<SysMenu> listThree = list(wrapperThree);
                //将三级菜单转换类型,封装进二级菜单的对象中
                List<SysMenuDto> collect1 = listThree.stream().map((itemThree) -> {
                    SysMenuDto sysMenuDtoThree=new SysMenuDto();
                    BeanUtils.copyProperties(itemThree,sysMenuDtoThree);
                    return sysMenuDtoThree;
                }).collect(Collectors.toList());
                sysMenuDtoTwo.setChildren(collect1);
                return sysMenuDtoTwo;
            }).collect(Collectors.toList());
            sysMenuDto.setChildren(collect);
        }
        return ResponseEntity.ok(R.success(sysMenuDtoList));
    }

    /**
     * 新增菜单的功能
     *
     * @param sysMenu
     * @return
     */
    @Override
    public ResponseEntity<R<String>> addSysMenu(SysMenu sysMenu) {
        save(sysMenu);
        return ResponseEntity.ok(R.success("新增成功!"));
    }

    /**
     * 根据id获取单个菜单,用于回显数据
     *
     * @param id
     * @return
     */
    @Override
    public ResponseEntity<R<SysMenu>> getSysMenuByID(Integer id) {
        SysMenu byId = getById(id);
        return ResponseEntity.ok(R.success(byId));
    }
}
